#include <stddef.h>
#include "linkedlist.h"

void linkedList :: add(int item)
{
  linkedListItem *anItemPtr = new linkedListItem;
  anItemPtr->value = item;

  // If headPtr is null then that means the list is empty so make this item the head and tail of the list
  if (headPtr == NULL){
    headPtr = anItemPtr;
    tailPtr = anItemPtr;
  } else {

    // Otherwise add to tail of the list
    tailPtr->nextPtr = anItemPtr;
    tailPtr = anItemPtr;
  }
}

linkedListItem* linkedList :: head() {return headPtr;}

linkedListItem* linkedList :: tail() {return tailPtr;}

linkedListItem* linkedList :: find(int val) {
  linkedListItem *eachItemPtr = headPtr;
  while(eachItemPtr) {
    if (eachItemPtr->value == val) return eachItemPtr;
    eachItemPtr = eachItemPtr->nextPtr;
  }
}

void linkedList :: clear() {
  linkedListItem *eachItemPtr = headPtr;
  linkedListItem *nextItemPtr;

  while(eachItemPtr) {
    nextItemPtr = eachItemPtr->nextPtr;
    delete eachItemPtr;
    eachItemPtr = nextItemPtr;
  }
  
}

linkedList :: ~linkedList() {
  clear();
}
